﻿using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;
using System;

namespace SHomeWorkshop.LunarPresentation.Dialogs
{
    /// <summary>
    /// Interaction logic for ExRecordInfo.xaml
    /// </summary>
    public partial class ExRecordInfo : Window
    {
        /// <summary>
        /// 创建一个记录（公式）信息对话框。
        /// </summary>
        /// <param name="keyWord">初始关键词。</param>
        /// <param name="authorInfo">初始作者信息。</param>
        /// <param name="comment">初始备注文本。</param>
        public ExRecordInfo(string keyWord, string authorInfo, string comment)
        {
            InitializeComponent();
            tbxKeyWord.Text = (keyWord == null) ? "" : keyWord;
            tbxAuthorInfo.Text = (authorInfo == null) ? "" : authorInfo;
            tbxComment.Text = (comment == null) ? "" : comment;

            tbxKeyWord.SelectAll();
            tbxKeyWord.Focus();

            //读取默认作者信息
            XDocument xDoc;
            if (System.IO.File.Exists(Globals.pathOfDefAuthorInfoXmlFile))
            {
                xDoc = XDocument.Load(/*"file:///" +加这个会出错！！*/ Globals.pathOfDefAuthorInfoXmlFile);

                if (xDoc.Root != null)
                {
                    tbxAuthorInfo.Text = xDoc.Root.Value;
                }
            }
        }

        private void btnSaveToDataBase_Click(object sender, RoutedEventArgs e)
        {
            #region 保存为默认作者信息。
            if (ckxSaveAsDefAuthorInfo.IsChecked == true)
            {
                XDocument xDoc;
                if (System.IO.File.Exists(Globals.pathOfDefAuthorInfoXmlFile))
                {
                    xDoc = XDocument.Load(/*"file:///" +加这个会出错！！*/ Globals.pathOfDefAuthorInfoXmlFile);

                    if (xDoc.Root == null)
                    {
                        XElement rootElement = new XElement("DefAuthorInfo");
                        xDoc.Add(rootElement);
                    }
                }
                else
                {
                    xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

                    XElement rootElement = new XElement("DefAuthorInfo");
                    xDoc.Add(rootElement);
                }

                xDoc.Root.SetValue(tbxAuthorInfo.Text);

                try
                {
                    xDoc.Save(Globals.pathOfPreferences);
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(Globals.appName,
                        "保存默认作者信息时出错。", "异常信息如下：\r\n" + ex.Message,
                        ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            #endregion

            if (tbxKeyWord.Text.Length <= 0)
            {
                MessageBox.Show("请输入关键词。", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (tbxAuthorInfo.Text.Length <= 0)
            {
                tbxAuthorInfo.Text = "<未设置>";
            }

            if (tbxComment.Text.Length <= 0)
            {
                tbxComment.Text = "<未设置>";
            }

            this.DialogResult = true;
            this.Close();
        }

        public string KeyWord { get { return tbxKeyWord.Text; } }

        public string AuthorInfo { get { return tbxAuthorInfo.Text; } }

        public string Comment { get { return tbxComment.Text; } }

        private void tbxComment_GotFocus(object sender, RoutedEventArgs e)
        {
            tbxComment.SelectAll();
        }

        private void tbxAuthorInfo_GotFocus(object sender, RoutedEventArgs e)
        {
            tbxAuthorInfo.SelectAll();
        }

        private void tbxKeyWord_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    {
                        btnSaveToDataBase_Click(null, null);
                        break;
                    }
                case Key.Escape:
                    {
                        this.DialogResult = false;
                        this.Close();
                        break;
                    }
            }
        }

        private void tbxAuthorInfo_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    {
                        btnSaveToDataBase_Click(null, null);
                        break;
                    }
                case Key.Escape:
                    {
                        this.DialogResult = false;
                        this.Close();
                        break;
                    }
            }
        }

        private void tbxComment_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    {
                        btnSaveToDataBase_Click(null, null);
                        break;
                    }
                case Key.Escape:
                    {
                        this.DialogResult = false;
                        this.Close();
                        break;
                    }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
